﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Actya.Web.Infrastructure.ElFinderConnector.Utils
{
    public static class FileUtils
    {
		//public static string GetMimeType(this FileInfo file)
		//{
		//    return GetMimeType(file.Extension.ToLower());
		//}

		//public static string GetMimeType(string ext)
		//{
		//    string mimeType = "application/unknown";            
		//    Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext);
		//    if (regKey != null && regKey.GetValue("Content Type") != null)
		//        mimeType = regKey.GetValue("Content Type").ToString();
		//    return mimeType;
		//}

        public static bool IsImage(this FileInfo file)
        {
            return new[] { ".jpg", ".jpeg", ".png", ".gif", ".bmp" }.Contains(file.Extension.ToLowerInvariant());
        }

        public static string ThumbUrl(this FileInfo file)
        {
            var thumbName = file.FullName.Hash() + ".png";
            if (File.Exists(Path.Combine(Configuration.Configuration.TmbDir, thumbName)))
                return string.Format("{0}/{1}", Configuration.Configuration.TmbUrl, thumbName);
            else return null;
        }

		public static void RemoveThumbFor(string filePath)
		{
			var thumbName = filePath.Hash() + ".png";
			var thumbPath = Path.Combine(Configuration.Configuration.TmbDir, thumbName);
			if (File.Exists(thumbPath))
			{
				File.Delete(thumbPath);
			}
		}

        public static string Duplicate(this FileInfo file)
        {
            var parentPath = file.DirectoryName;

            var name = file.Name;

            var ext = string.Empty;

            var nameArr = name.Split(".".ToCharArray());

            if (nameArr.Length > 1)
            {

                ext = "." + nameArr[nameArr.Length - 1];
                name = name.Remove(name.LastIndexOf("."));
            }

            var newName = string.Format(@"{0}\{1} copy{2}", parentPath, name, ext);

            if (!File.Exists(newName))
            {
                file.CopyTo(newName);
            }
            else
            {
                for (int i = 1; i < 100; i++)
                {
                    newName = string.Format(@"{0}\{1} copy {2}{3}", parentPath, name, i, ext);
                    if (!File.Exists(newName))
                    {
                        file.CopyTo(newName);
                        break;
                    }
                }
            }

            return newName;
        }

        public static string PathFromRoot(this FileSystemInfo dir)
        {
            var path = dir.FullName;
            return path.Replace(Configuration.Configuration.RootPath, "");
            /*
            string result = dir.Name;
            while (dir.FullName != Configuration.Configuration.RootPath)
            {
                var parent = dir.Parent;
                result = parent.Name + @"\" + result;
                dir = parent;
            }

            return result;
            */
        }

        public static UserFileAccessRights GetRights(this FileSystemInfo dir)
        {

            return new UserFileAccessRights(dir.FullName);
        }
    }
}
